package nokia.iap.example.prodcatalog;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.nokia.mid.payment.IAPClientPaymentException;
import com.nokia.mid.payment.IAPClientPaymentManager;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;
import nokia.iap.ui.lib.prodcatalog.ProductCatalogForm;
import nokia.iap.ui.lib.prodcatalog.ProductCatalogListener;
/**
 * @author zinin
 */
public class ProductCatalog extends MIDlet implements CommandListener, ProductCatalogListener{
    private ProductCatalogForm catalog;
    public static final Command exitCommand = new Command( "Exit the app", Command.EXIT, 1 );
    private final String[] prodList = new String[]{"588547", "588549", "588550", "588551"};
    
    public void startApp() {
        try {
            // full touch device detected
            com.nokia.mid.ui.VirtualKeyboard.hideOpenKeypadCommand(true);
        } catch (Throwable e) {
        }
        catalog = new ProductCatalogForm(this, "example", prodList);
        catalog.addCommand(exitCommand);
        catalog.setCommandListener(this);
        Display.getDisplay(this).setCurrent(catalog);
        try {
            catalog.rebuildCatalog();
        } catch (IAPClientPaymentException ex) {
        }
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        exitMIDlet();
    }

    public void exitMIDlet(){
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        if( c == exitCommand ){
            exitMIDlet();
        }
    }
 
    public void useUnlockedProduct(String productID) {
        StringBuffer fileInfo = new StringBuffer();
        try {
            String lockedFname = "data.txt";
            InputStream input = IAPClientPaymentManager.getIAPClientPaymentManager().
                            getDRMResourceAsStream(ProductCatalogForm.DRM_PATH +
                                                            productID + "/" + lockedFname);
            int data = input.read();
            while(data != -1) {
              fileInfo.append((char)data);
              data = input.read();
            }
            input.close();
        } catch (IOException ex) {
            fileInfo.append("ERROR exception\n");
            fileInfo.append(ex.getMessage() + "\n");
            fileInfo.append("while reading DRM file");
        } catch (IAPClientPaymentException ex) {
            fileInfo.append("ERROR IAPClientPaymentManager\n");
            fileInfo.append(ex.getMessage() + "\n");
            fileInfo.append("while reading DRM file");
        }finally{
            Alert alert = new Alert("", fileInfo.toString(), null, AlertType.CONFIRMATION);
            alert.setTimeout(Alert.FOREVER);
            Display.getDisplay(this).setCurrent(alert);
        }
    }

    public Display getDisplay() {
        return Display.getDisplay(this);
    }
}
